NinjaPicker = LibStub("AceAddon-3.0"):NewAddon("NinjaPicker", "AceConsole-3.0", "AceEvent-3.0")

local self = NinjaPicker

local options = {
	type = "group",
	handler = NinjaPicker,
	name = "NinjaPicker",
	desc = "Ninjas really do pick those locks best...",
	args = {
		new = {
			type = "input",
			name = "New",
			desc = "Create a new message for the current group",
			order = 7,
			get = "GetSelected",
			set = "CreateNewMessage",
			usage = "Use %n for your name and %l for your lockpicking level",
			width = "full",
		},
		announce = {
			type = "execute",
			name = "Announce",
			desc = "Randomly announce a message from the selected group",
			order = 2,
			func = "Announce",
		},
		set = {
			type = "select",
			name = "Group",
			desc = "Choose what group you want to use",
			order = 3,
			get = "GetCurrentGroup",
			set = "SetCurrentGroup",
			style = "dropdown",
			width = "full",
		},
		newGroup = {
			type = "input",
			name = "New Group",
			desc = "Create a new group",
			order = 4,
			get = false,
			set = "CreateNewGroup",
			usage = "<group name>",
			width = "full",
		},
		remGroup = {
			type = "execute",
			name = "Remove",
			desc = "Remove selected group?",
			order = 3,
			func = "RemoveGroup",
			disabled = function() return not self.db.profile.group end,
			confirm = true,
			width = "half",
		},
		remSelect = {
			type = "select",
			name = "Select",
			order = 5,
			get = "GetSelected",
			set = "SetSelected",
			style = "dropdown",
			width = "full",
		},
		remExec = {
			type = "execute",
			name = "Remove",
			desc = "Remove selected message from this group?",
			order = 6,
			func = "RemoveSelected",
			disabled = function() return not self.db.profile.selected end,
			confirm = true,
			width = "half",
		},
		autoSwitch = {
			type = "toggle",
			name = "Auto Switch",
			desc = "Toggle automatically switching groups based on zone",
			order = 1,
			get = "GetAutoSwitch",
			set = "SetAutoSwitch",
		},
		setGroupZone = {
			type = "select",
			name = function() return "Zone - "..GetZoneText() end,
			desc = "Toggle what group you want to automatically switch to for the current zone",
			order = 8,
			get = "GetZoneGroup",
			set = "SetZoneGroup",
			hidden = function() return not self.db.profile.auto end,
		},
		clearZones = {
			type = "execute",
			name = "Clear Zones",
			desc = "Remove all group-specific zones from the database",
			order = 8,
			func = "ClearZones",
		},
	}
}

local defaults = {
	profile = {
		groups = {
			["Default"] = {
				"Picking all lockboxes level %l or lower, tips appreciated",
			},
		},
		group = "Default",
		zones = {},
		selected = nil,
		auto = false,		
	},
}

local function GetAllGroups()
	local newTable = {}
	for name in pairs(self.db.profile.groups) do
		newTable[name] = name
	end

	return newTable
end

local function GetGroupMessages()
	local newTable = {}
	local group = self.db.profile.group
	for i, msg in ipairs(self.db.profile.groups[group]) do
		newTable[msg] = msg
	end
	
	return newTable
end

local function GetPickLevel()
	for i=1, GetNumSkillLines() do
		local skillName, isHeader, isExpanded, skillRank = GetSkillLineInfo(i) 
		if skillName == "Lockpicking" then
			return tostring(skillRank)
		end
	end
end

function NinjaPicker:OnInitialize()
	self.db = LibStub("AceDB-3.0"):New("NinjaPickerDB", defaults, "Defaults")
	
	options.args.set.values = GetAllGroups
	options.args.remSelect.values = GetGroupMessages
	options.args.setGroupZone.values = GetAllGroups
	
	LibStub("AceConfig-3.0"):RegisterOptionsTable("NinjaPicker", options)
	self.optionsFrame = LibStub("AceConfigDialog-3.0"):AddToBlizOptions("NinjaPicker", "NinjaPicker")

	self:RegisterChatCommand("npick", "Announce")
end

function NinjaPicker:OnEnable()
	self:RegisterEvent("ZONE_CHANGED_NEW_AREA")
end

function NinjaPicker:Announce()
	local group = self.db.profile.group
	local msg = self.db.profile.groups[group][math.random(1, #self.db.profile.groups[group])]
	local level, name = GetPickLevel(), UnitName("player")
	SendChatMessage(msg:gsub("%%n", name):gsub("%%l", level), "CHANNEL", nil, "2")
end

function NinjaPicker:ZONE_CHANGED_NEW_AREA()
	local zone = GetZoneText()
	if self.db.profile.auto and self.db.profile.zones[zone] then
		self.db.profile.group = self.db.profile.zones[zone]
		self:Print("Auto-switched to group: "..self.db.profile.group)
	end
end

function NinjaPicker:CreateNewMessage(info, value)
	local group = self.db.profile.group
	if not self.db.profile.groups[group][value] then
		table.insert(self.db.profile.groups[group], value)
	end
end

function NinjaPicker:CreateNewGroup(info, value)
	self.db.profile.groups[value] = {}
end

function NinjaPicker:GetCurrentGroup(info)
	return self.db.profile.group or nil
end

function NinjaPicker:SetCurrentGroup(info, value)
	self.db.profile.group = value
end

function NinjaPicker:GetSelected(info)
	return self.db.profile.selected or nil
end

function NinjaPicker:SetSelected(info, value)
	self.db.profile.selected = value
end

function NinjaPicker:RemoveSelected(info)
	local group = self.db.profile.group
	local selected = self.db.profile.selected
	for i, msg in ipairs(self.db.profile.groups[group]) do
		if msg == selected then
			table.remove(self.db.profile.groups[group], i)
		end
	end
end

function NinjaPicker:RemoveGroup(info)
	local group = self.db.profile.group
	self.db.profile.groups[group] = nil
	
	local groups = GetAllGroups()
	for name in pairs(groups) do
		if name then
			self.db.profile.group = name
		end
	end
end

function NinjaPicker:GetAutoSwitch(info)
	return self.db.profile.auto or nil
end

function NinjaPicker:SetAutoSwitch(info, value)
	self.db.profile.auto = value
end

function NinjaPicker:GetZoneGroup(info)
	local zone = GetZoneText()
	return self.db.profile.zones[zone] or nil
end

function NinjaPicker:SetZoneGroup(info, value)
	local zone = GetZoneText()
	if not self.db.profile.zones[zone] then
		self.db.profile.zones[zone] = {}
	end
	
	self.db.profile.zones[zone] = value
end

function NinjaPicker:ClearZones(info)
	self.db.profile.zones = {}
end